//
//  MCYearBillListViewController.swift
//  StoreManage
//
//  Created by MC on 2018/5/21.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCYearBillListViewController: MCViewController {
    
    private var pageNum = 1

    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action

    
    // MARK: - Network
    func sendNetworking() {
        let para = [
            "pageNum" : pageNum
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_findMonthReceipts, para: para as [String : AnyObject], vc: self,hudSuperView: nil,isHiddenTabbar: true, success: { (response) in
            
            let dataArray = response["data"] as? NSArray ?? []
            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
            
            if self.pageNum == 1 {
                self.dataArrayM.removeAllObjects()
            }

            
            if dataArray.count + self.dataArrayM.count == 0 {
                self.tableView.showBgImage()
            } else {
                self.tableView.hideBgImage()
                
                for dict in dataArray {
                    let model = MCBillListModel()
                    model.setValuesForKeys(dict as? [String:Any] ?? [:])
                    self.dataArrayM.add(model)
                }
                
                self.tableView.reloadData()
            }
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "历史收款"
        self.view.backgroundColor = UIColor.white
        
        self.tableView.mj_header = MCRefreshHeader.init(refreshingBlock: {
            self.pageNum = 1
            self.sendNetworking()
        })
        
        self.tableView.mj_footer = MCRefreshFooter.init(refreshingBlock: {
            self.pageNum += 1
            self.sendNetworking()
        })

    }
    
    func initUI() {
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view)
        }        
    }
    
    // MARK: - Setter & Getter
    lazy private var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
}

extension MCYearBillListViewController : UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArrayM.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 150
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = MCBillListCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.backgroundColor = UIColor.MCGray_light
        cell.model = self.dataArrayM[indexPath.row] as! MCBillListModel
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let model = self.dataArrayM[indexPath.row] as! MCBillListModel
        
        let vc = MCMonthBillListViewController()
        vc.dateStr = model.date
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }
}

